<?php
class FeeorderModel extends Model{
	private $table1 = 'fee_order';
	private $table2 = 'products';
	private $table3 = 'fee';
	private $table4 = 'membership';
	//视图
	private $table5 = 'feeorder';
	
	/**
	 * 获取订单详细信息
	 */
	public function Feeorderdetail($where){
		$sql = "SELECT fo.*,p.products_name,p.contact_person,p.contact_tel,f.`name` as f_name FROM `".$this->table1."` fo LEFT JOIN ".$this->table2." p ON o.products_id=p.products_id LEFT JOIN ".
				 $this->table3." f ON fo.fee_id=f.fee_id WHERE fo.fee_order_id=".$where.";";
		 $result = M($this->table1)->query($sql);
		// echo $sql;die;
		 //echo M($this->table1)->getLastSql();die;
		 return $result[0];
	}
	
	/**
	 * 获取会员订单列表
	 * @param $where $order $limit $field
	 */
	public function getMemberOrder($where,$order,$field){
		 return $this->table("`".$this->table1."` fo")->join($this->table2." p ON fo.products_id=p.products_id")->join($this->table3." f ON fo.fee_id=f.fee_id")->where($where)->field($field)->order($order)->select();
		 //echo $this->getLastSql();die;
	}
	
	/**
	 * 后台获取订单列表
	 * @param $where $order $limit $field
	 * @return array
	 */
	public function getFeeorderList($where,$order,$limit){
		//return $this->table("`".$this->table1."` fo")->join($this->table2." p ON fo.products_id=p.products_id")->join($this->table3." f ON fo.fee_id=f.fee_id")->join($this->table4." m ON fo.uid=m.uid")->where($where)->field($field)->order($order)->limit($limit)->select();
		return M($this->table5)->where($where)->order($order)->limit($limit)->select();
		//echo   M($this->table5)->getLastSql();die;
		
	}
	
	/**
	 * 后台获取订单个数
	 * @param $where
	 * @return array
	 */
	public function getOrderCount($where){
		//return  $this->table("`".$this->table1."` fo")->join($this->table2." p ON fo.products_id=p.products_id")->join($this->table3." f ON fo.fee_id=f.fee_id")->where($where)->count();
		return M($this->table5)->where($where)->count();
		//echo  $this->getLastSql();die;
	}
	
	/**
	 * 后台获取订单详情
	 * @param $where $field
	 */
	public function getOrderDetail($where,$field){
		$this->table("`".$this->table1."` fo")->join($this->table2." p ON fo.products_id=p.products_id")->join($this->table3." f ON fo.fee_id=f.fee_id")->where($where)->field($field)->find();
	}
	
	/**
	 * 按照天数获取产品个数
	 * @param string $where
	 */
	public function getFcByday($where=1,$limit,$order){
		if($limit){
			$sql = "select DATE_FORMAT(f.add_date,'%Y-%m-%d') day, count(*) c from ".$this->table1." f LEFT JOIN ".$this->table2." p ON f.products_id=p.products_id WHERE ".$where." group by DATE_FORMAT(f.add_date,'%Y%m%d') ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "select DATE_FORMAT(f.add_date,'%Y-%m-%d') day, count(*) c from ".$this->table1." f LEFT JOIN ".$this->table2." p ON f.products_id=p.products_id  WHERE ".$where." group by DATE_FORMAT(f.add_date,'%Y%m%d') ORDER BY ".$order;
		}
		//echo $sql;die;
		return M($this->table1)->query($sql);
	}
	
	
	/**
	 * 获取按天数获取产品的个数
	 * @param string  $where
	 */
	public function getFc($where=1){
		$sql = "select DATE_FORMAT(f.add_date,'%Y-%m-%d') day, count(*) c from ".$this->table1." f LEFT JOIN ".$this->table2." p ON f.products_id=p.products_id  WHERE ".$where." group by DATE_FORMAT(f.add_date,'%Y%m%d') ";
		$result = M($this->table1)->query($sql);
		return count($result);
	}
	
	/**
	 * 获取统计商品的详情
	 */
	public function getFcDeatil($where,$limit,$order){
		if($limit){
			$sql = "SELECT  p.products_name, f.*, fe.fee_name  FROM ".$this->table1." f LEFT JOIN ".$this->table2." p ON f.products_id=p.products_id LEFT JOIN ".$this->table3." fe ON f.fee_id=fe.fee_id WHERE ".$where." ORDER BY ".$order." LIMIT ".$limit;
		}else{
			$sql = "SELECT  p.products_name, f.*, fe.fee_name  FROM ".$this->table1." f LEFT JOIN ".$this->table2." p ON f.products_id=p.products_id LEFT JOIN ".$this->table3." fe ON f.fee_id=fe.fee_id WHERE ".$where." ORDER BY ".$order;
		}
		//echo $sql;die;
		return M($this->table1)->query($sql);
	}
}